package whf.shop.utils;

import java.util.List;

import javax.servlet.http.HttpSession;

import whf.framework.dao.hibernate.HibernateHelper;
import whf.framework.dao.param.IParam;
import whf.framework.dao.param.LongParam;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.security.HibernateSpringUserManager;
import whf.framework.security.UserContext;
import whf.framework.security.entity.User;
import whf.framework.security.service.UserServiceImp;
import whf.framework.util.Pair;
import whf.framework.util.StringUtils;
import whf.framework.util.ThreadContext;
import whf.framework.util.Utils;
import whf.framework.web.WebContextHolder;
import whf.shop.entity.Currency;
import whf.shop.entity.Member;
import whf.shop.entity.Order;
import whf.shop.entity.Shop;
import whf.shop.service.CurrencyServiceImp;
import whf.shop.service.MemberServiceImp;

/**
 * @author wanghaifeng
 * @create Jan 12, 2007 10:11:43 AM
 * 
 */
public final class ShopUtils {
	private static Log log = LogFactory.getLog(ShopUtils.class);
	public final static String SESSION_SHOP = "SESSION_SHOP";
	public final static String SESSION_MEMBER = "SESSION_MEMBER";
	public final static String SESSION_ORDER = "SESSION_ORDER";
	public final static String SESSION_CURRENCY = "SESSION_CURRENCY";
	
	/**
	 * 当前回话中的成员
	 * @modify wanghaifeng Jan 12, 2007 10:13:02 AM
	 * @param session
	 * @return
	 */
	public static Member getCurrentMember(HttpSession session){
		Member m = (Member)session.getAttribute(SESSION_MEMBER);
		if(m == null){
			User user = ThreadContext.getUserInUserContext();
			if(user != null){
				try{
					m = MemberServiceImp.getMemberService().findByUser(user);
					session.setAttribute(SESSION_MEMBER, m);
				} catch(Exception e){
					log.error(e);
				}
			}
		}
		return m;
	}
	
	/**
	 * 当前Order
	 * @modify wanghaifeng Jan 12, 2007 11:24:31 AM
	 * @param session
	 * @return
	 */
	public static Order getCurrentOrder(HttpSession session){
		Order order = (Order)session.getAttribute(SESSION_ORDER);
		if(order == null){
			order = new Order();
			session.setAttribute(SESSION_ORDER, order);
		}
		return order;
	}
	
	/**
	 * 获取当前的currency
	 * @modify wanghaifeng Mar 31, 2007 3:40:29 PM
	 * @param session
	 * @return
	 */
	public static Currency getCurrentCurrency(HttpSession session){
		Currency cur = (Currency)session.getAttribute(SESSION_CURRENCY);
		if(cur == null){
			Member m = getCurrentMember(session);
			if(m != null){
				cur = m.getCurrency();
				session.setAttribute(SESSION_CURRENCY, cur);
			}
		}
		if(cur == null){
			try{
				cur = CurrencyServiceImp.getCurrencyService().findBaseCurrency();
				session.setAttribute(SESSION_CURRENCY, cur);
			}catch(Exception e){
				log.error(e);
			}
		}
		return cur;
	}
	
	/**
	 * @modify wanghaifeng Mar 31, 2007 3:52:07 PM
	 * @param session
	 * @param cur
	 */
	public static void setCurrentCurrency(HttpSession session, Currency cur){
		if(cur == null){
			session.removeAttribute(SESSION_CURRENCY);
		} else {
			session.setAttribute(SESSION_CURRENCY, cur);
		}
	}
	
	/**
	 * 获取当前的商店
	 * @modify wanghaifeng Jan 24, 2007 9:22:11 AM
	 * @param session
	 * @return
	 */
	public static Shop getCurrentShop(HttpSession session){
		return (Shop)session.getAttribute(SESSION_SHOP);
	}
	
	/**
	 * 格式化价格
	 * @modify wanghaifeng Mar 31, 2007 4:10:49 PM
	 * @param session
	 * @param price
	 * @return
	 */
	public static Pair<Currency, String> formatPrice(HttpSession session, double price){
		Currency cur = getCurrentCurrency(session);
		double output = CurrencyUtils.calculate(price, cur);
		String symbol = cur == null?"": cur.getSymbol();
		return new Pair<Currency, String>(cur, symbol + StringUtils.format(output, null));
	}
	
	/**
	 * 搜索绑定到会员的用户
	 * @modify wanghaifeng Apr 7, 2007 11:02:11 AM
	 * @return
	 */
	public static User findBindUserForMember() {
		try{
			return (User)UserServiceImp.getUserService().findByCode("whf.shop.entity.member");
		} catch(Exception e){
			log.warn(e);
			return null;
		}
	}
	
	/**
	 * 获取会员的上下文
	 * @modify wanghaifeng Apr 7, 2007 11:05:16 AM
	 * @param session
	 * @return
	 */
	public static UserContext getUserContext(HttpSession session){
		return WebContextHolder.getWebContext().getUserContext(findBindUserForMember());
	}
	
	public static void main(String[] args) throws Exception{
		User user = ShopUtils.findBindUserForMember();
		System.out.println(user);
		List<IParam> params = Utils.newArrayList();
		params.add(new LongParam("user_id", user == null? 0:user.getId()));
		List list = HibernateHelper.getNamedQueryResult("find-role-by-user",params);
		System.out.println(list);
		System.out.println(HibernateSpringUserManager.getUserManager().findRoles(user));
	}
}
